<template>
  <!--
    作者：luoyiming
    时间：2020-06-04
    描述：插件中心-积分商城-商品设置-添加商品
  -->
  <div class="pb50">
    <el-form size="small" ref="form" :model="form" label-width="150px" v-if="!loading">
      <!--商品信息-->
      <Info></Info>

      <!--规格-->
      <Spec></Spec>

      <!--其它-->
      <Other></Other>

      <!--提交-->
    </el-form>

    <div class="common-button-wrapper">
      <el-button size="small" type="info" @click="cancelFunc">取消</el-button>
      <el-button size="small" type="primary" @click="onSubmit" :loading="loading">提交</el-button>
    </div>
  </div>
</template>
<script>
import DiscountApi from '@/api/discount';
import Info from './part/Info.vue';
import Spec from './part/Spec.vue';
import Other from './part/Other.vue';
import { formatModel } from '@/utils/base.js';
export default {
  components: {
    /*产品基本信息*/
    Info,
    /*规格*/
    Spec,
    /*其它*/
    Other
  },
  data() {
    return {
      /*是否正在加载*/
      loading: true,
      /*表单*/
      form: {
        /*商品ID*/
        discount_product_id: 0,
        /*商品状态*/
        status: 10,
        /*商品名称*/
        product_name: '',
        /*图片*/
        product_image:'',
        /*排序*/
        sort: 100,
        /*规格*/
        product_sku_id: 0,
        /*限购数量*/
        limit_num: 1,
        /*商品表格*/
        tableData: [],
        /*活动名称*/
        active_name:'',

        activity_type:1,
        limit_user:0,
        explain:'',
        first_price:'',
        start_buy_num:'',
        discount_moeny:0,
        buy_num:'',
        gift_num:'',
        tag:'',
        rebate_json:[
          {
            'num':1,
            'rebate':100
          },
          {
            'num':2,
            'rebate':100
          }
        ]
      }
     
    };
  },
  provide: function() {
    return {
      form: this.form,
      type: 'add'
    };
  },
  created() {
    this.form.discount_product_id = this.$route.query.discount_product_id;
    this.getData();
  },
  methods: {

    /*获取商品*/
    getData() {
      let self = this;
      DiscountApi.detailProduct(
        {
          discount_product_id: self.form.discount_product_id
        },
        true
      ).then(res => {
        self.form.product_name=res.data.model.product.product_name;
        self.form.product_image=res.data.model.product.image[0].file_path;
        self.form.product_id=res.data.model.product.product_id;
        self.form.limit_num=res.data.model.limit_num;
        self.form.stock=res.data.model.stock;
        self.form.active_name = res.data.model.active.title;

        self.form.rebate_json=res.data.model.rebate_json;
        self.form.buy_num=res.data.model.buy_num;
        self.form.activity_type=res.data.model.activity_type;
        self.form.limit_user=res.data.model.limit_user;
        self.form.explain = res.data.model.explain;
        self.form.first_price=res.data.model.first_price;
        self.form.start_buy_num=res.data.model.start_buy_num;
        self.form.gift_num=res.data.model.gift_num;
        self.form.discount_moeny=res.data.model.discount_moeny;
        self.form.tag=res.data.model.tag;

        res.data.model.discountSku.forEach(item=>{
          item.product_name= self.form.product_name;
          item.spec_name=item.product_attr;
          item.spec_type=res.data.model.product.spec_type;
        });
        self.form.tableData=res.data.model.discountSku;
        self.loading = false;
      });
    },

    /*提交表单*/
    onSubmit() {
      let self = this;
      self.$refs.form.validate(valid => {
        if (valid) {
          let params = {
            product: {}
          };
          params.product_id = self.form.product_id;
          params.status = self.form.status;
          params.sort = self.form.sort;
          params.limit_num = self.form.limit_num;

          params.rebate_json = self.form.rebate_json;
          params.buy_num = self.form.buy_num;
          params.activity_type = self.form.activity_type;
          params.limit_user = self.form.limit_user;
          params.explain = self.form.explain;
          params.first_price = self.form.first_price;
          params.gift_num = self.form.gift_num;
          params.discount_moeny = self.form.discount_moeny;
          params.start_buy_num = self.form.start_buy_num;
          params.tag = self.form.tag;
          params.discount_product_id=self.form.discount_product_id;
          params.product_id=self.form.product_id;
          params.product.spec_list = self.tableFormet(self.form.tableData);
          self.loading = true;
          DiscountApi.saveProduct(params, true)
            .then(data => {
              self.loading = false;
              self.$message({
                message: '恭喜你，修改成功',
                type: 'success'
              });
              self.cancelFunc();
            })
            .catch(error => {
              self.loading = false;
            });
        }
      });
    },

    /*表格数据格式化*/
    tableFormet(list) {
      list.forEach(item=>{
        item.product_attr=item.spec_name;
      });
      return list;
    },

    /*取消*/
    cancelFunc() {
      this.$router.back(-1);
    }
  }
};
</script>
<style>
.edit_container {
  font-family: 'Avenir', Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
}

.ql-editor {
  height: 400px;
}
</style>
